package com.helei.consumser.test;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.*;

/**
 * @author helei
 * @description test
 * @date 2020/7/14 15:19
 */
public class test {
    //获取json
    public static String getJson() {
        String jsonStr = "";
        try {
            File file = new File("C:\\Users\\Administrator\\Desktop\\cloud2020\\nocas-consumer\\src\\main\\java\\com\\helei\\consumser\\json.json");
            FileReader fileReader = new FileReader(file);
            Reader reader = new InputStreamReader(new FileInputStream(file), "Utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (Exception e) {
            return null;
        }
    }

//2、解析多层json

    public static void main(String[] args) {
        //System.out.println(getJson());
      //  JSONObject json = JSONObject.fromObject(getJson());
      //  System.out.println(json.getJSONObject("data").getJSONArray("result"));
        JsonParser jp = new JsonParser();
        //将json字符串转化成json对象
        JsonObject jo = jp.parse(getJson()).getAsJsonObject();
        //获取message对应的值
        String message = jo.get("code").getAsString();
        System.out.println("message：" + message);

    }
}
